package com.rr.sc.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.rr.sc.bo.UserBo;
import com.rr.sc.model.User;

public class MyUserDetailsService implements UserDetailsService {

    private UserBo userBo;

    @Autowired
    public void setUserService(UserBo userBo) {
	this.userBo = userBo;
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
	User user = userBo.get(username);

	if (user != null) {
	    return new MyUserDetails(user, new String[] { "ROLE_CONNECT", "ROLE_USER", "ROLE_ADMIN" });
	} else {
	    throw new BadCredentialsException("User not found");
	}
    }
}
